// pagesA/memberlist/index.js

const app = getApp();

import {
  homelist,
  getHyly
} from '../../utils/http';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    pages:0,
    current:0,
    noData: false,
    size:10,
    d:[],
    keyword:'',
    loadend:false,
    hyly: [],
    hylyName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init()
    getHyly({current:1,size:10}).then(r => {
      this.setData({
        hyly: r.data.records
      })
    })
  },
  searchclick:function(e)
  {
    console.log(e,'===========')
    this.setData({
      keyword:e.detail
    })
    this.init()
  },
  setHyly (e) {
    let t = this;
    let hylyName = e.currentTarget.dataset.name;
    t.setData({
      hylyName
    })
    this.init()
  },
  async init() {
    let t = this;
    t.setData({
      page: 1,
      pages: 0,
      current: 0,
      noData: false,
      kong: false
    })
    t.getMore();
  },


  getMore() {
    let t = this;
    var promise = new Promise((resolve, reject) => {
      if (t.data.pages >= t.data.current) {
        homelist({
          page: t.data.page,
          pageSize: t.data.size,
          keyWord:t.data.keyword,
          industry: t.data.hylyName
        }).then(r => {
          if (r.httpCode == 200 || r.code == 0) {
            let d;
            if (t.data.page == 1) {
              d = [];
            } else {
              d = t.data.d;
            }
            if (r.data.records.length) {
              r.data.records.forEach(i => {
                i.manageRangeName = i.manageRangeName ? i.manageRangeName.split(',') : '';
                i.imagedots = 0
                d.push(i)
              })
              if (r.data.records.length < r.data.size) {
                t.setData({
                  noData: true,
                })
              } else {
                t.setData({
                  noData: false,
                })
              }
            } else {
              t.setData({
                noData: true,
              })
            }
            t.setData({
              d,
              page: ++t.data.page,
              pages: r.data.pages,
              current: r.data.current,
              total:r.data.total,
              loadend:true
            })
          }

          resolve();
        })
      } else {
        t.setData({
          noData: true,
          loadend:true
        })
        resolve();
      }
    });
    return promise;
  },


  imgbindchange:function(e)
  {
    console.log(e)
    var index1 = e.detail.current
    var itemindex = e.target.dataset.index
    var dd = this.data.d
    dd.forEach((i, index) => {
      if(index == itemindex)
      {
        i.imagedots = index1
      }
    })
    this.setData({
      d:dd
    })
    console.log(this.data.d)

  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.noData) {
      this.getMore();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    // if(e.target)
    // {
    //   let name;
    //   let wxApp = app.config.wxApp;
    //   name = this.data.dqname + '分享给您' + e.target.dataset.item.name + '的名片';
    //   return app.share({
    //     name,
    //     url: '/pages/index/detail?id=' + e.target.dataset.item.id,
    //     img: wxApp.mpShare
    //   });
    // }
    

    return app.share();
  }
})